/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tests;

import java.awt.Color;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.GrayPaintScale;
import org.jfree.chart.renderer.PaintScale;
import org.jfree.chart.renderer.xy.XYBlockRenderer;
import org.jfree.data.DomainOrder;
import org.jfree.data.general.DatasetChangeListener;
import org.jfree.data.general.DatasetGroup;
import org.jfree.data.xy.XYZDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities; 

/** 
 * A simple demonstration application showing a chart created using  
 * the {@link XYBlockRenderer}. 
 * <br><br> 
 * TODO: The chart needs a display showing the value scale. 
 */ 
public class XYBlockChartDemo1 extends ApplicationFrame { 

    /** 
     * Constructs the demo application. 
     * 
     * @param title  the frame title. 
     */ 
    public XYBlockChartDemo1(String title) { 
        super(title); 
        JPanel chartPanel = createDemoPanel(); 
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); 
        setContentPane(chartPanel); 
    } 
     
    /** 
     * Creates a sample chart. 
     *  
     * @param dataset  the dataset. 
     *  
     * @return A sample chart. 
     */ 
    private static JFreeChart createChart(XYZDataset dataset) { 
        NumberAxis xAxis = new NumberAxis("X"); 
        xAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
        xAxis.setLowerMargin(0.0); 
        xAxis.setUpperMargin(0.0); 
        NumberAxis yAxis = new NumberAxis("Y"); 
        yAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
        yAxis.setLowerMargin(0.0); 
        yAxis.setUpperMargin(0.0); 
        XYBlockRenderer renderer = new XYBlockRenderer(); 
        PaintScale scale = new GrayPaintScale(-2.0, 1.0); 
        renderer.setPaintScale(scale); 
        XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer); 
        plot.setBackgroundPaint(Color.lightGray); 
        plot.setDomainGridlinesVisible(false); 
        plot.setRangeGridlinePaint(Color.white); 
        JFreeChart chart = new JFreeChart("XYBlockChartDemo1", plot); 
        chart.removeLegend(); 
        chart.setBackgroundPaint(Color.white); 
        return chart; 
    } 
     
    /** 
     * Creates a sample dataset. 
     */ 
    private static XYZDataset createDataset() { 
        return new XYZDataset() { 
            public int getSeriesCount() { 
                return 1; 
            } 
            public int getItemCount(int series) { 
                return 10000; 
            } 
            public Number getX(int series, int item) { 
                return new Double(getXValue(series, item)); 
            } 
            public double getXValue(int series, int item) { 
                return item / 100 - 50; 
            } 
            public Number getY(int series, int item) { 
                return new Double(getYValue(series, item)); 
            } 
            public double getYValue(int series, int item) { 
                return item - (item / 100) * 100 - 50; 
            } 
            public Number getZ(int series, int item) { 
                return new Double(getZValue(series, item)); 
            } 
            public double getZValue(int series, int item) { 
                double x = getXValue(series, item); 
                double y = getYValue(series, item); 
                return Math.sin(Math.sqrt(x * x + y * y) / 5.0); 
            } 
            public void addChangeListener(DatasetChangeListener listener) { 
                // ignore - this dataset never changes 
            } 
            public void removeChangeListener(DatasetChangeListener listener) { 
                // ignore 
            } 
            public DatasetGroup getGroup() { 
                return null; 
            } 
            public void setGroup(DatasetGroup group) { 
                // ignore 
            } 
            public Comparable getSeriesKey(int series) { 
                return "sin(sqrt(x + y))"; 
            } 
            public int indexOf(Comparable seriesKey) { 
                return 0; 
            } 
            public DomainOrder getDomainOrder() { 
                return DomainOrder.ASCENDING; 
            }         
        }; 
    } 
     
    /** 
     * Creates a panel for the demo. 
     *   
     * @return A panel. 
     */ 
    public static JPanel createDemoPanel() { 
        return new ChartPanel(createChart(createDataset())); 
    } 
     
    /** 
     * Starting point for the demonstration application. 
     * 
     * @param args  ignored. 
     */ 
    public static void main(String[] args) { 
        XYBlockChartDemo1 demo = new XYBlockChartDemo1("Block Chart Demo"); 
        demo.pack(); 
        RefineryUtilities.centerFrameOnScreen(demo); 
        demo.setVisible(true); 
    } 

}